package com.hillky.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hillky.springboot.controller.dto.LoginDTO;
import com.hillky.springboot.controller.request.BaseRequest;
import com.hillky.springboot.controller.request.LoginRequest;
import com.hillky.springboot.controller.request.PassRequest;
import com.hillky.springboot.entity.Admin;
import com.hillky.springboot.exception.ServiceException;
import com.hillky.springboot.mapper.AdminMapper;
import com.hillky.springboot.service.AdminService;
import com.hillky.springboot.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {


    private static final String SALT="hillky";

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> list(){
        return adminMapper.list();
    }

    @Override
    public PageInfo<Admin> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<Admin> Admins = adminMapper.listByCondition(baseRequest);
        return new PageInfo<>(Admins);
    }

    @Override
    public void save(Admin admin) {
        admin.setPassword(securePassword(admin.getPassword()));
        try {
            adminMapper.save(admin);
        }catch (DuplicateKeyException e){
            throw new ServiceException("用户名重复");
        }
    }

    @Override
    public Admin getById(Integer id) {
        Admin Admin=adminMapper.getById(id);
        return Admin;
    }

    @Override
    public void update(Admin admin) {
        admin.setUpdateTime(new Date());
        adminMapper.update(admin);
        return;
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
        return;
    }

    @Override
    public LoginDTO login(LoginRequest loginRequest) {
        String username = loginRequest.getUsername();
        String password = securePassword(loginRequest.getPassword());
        Admin admin=adminMapper.getByUsername(username);
        if(ObjectUtil.isEmpty(admin)){
            throw new ServiceException("用户名不存在");
        }
        if(!admin.getPassword().equals(password)){
            throw new ServiceException("密码错误");
        }
        //校验状态是否可以登录
        if(!admin.getStatus()){
            throw new ServiceException("账户状态异常，请联系管理员");
        }
        LoginDTO loginDTO = new LoginDTO();
        BeanUtils.copyProperties(admin,loginDTO);
        loginDTO.setToken(TokenUtils.genToken(String.valueOf(admin.getId()),admin.getPassword()));
        return loginDTO;
    }

    @Override
    public void changePassword(PassRequest passRequest) {
        //获取原数据库对象
        Admin admin = this.adminMapper.getByUsernameAndPassword(passRequest.getUsername(), passRequest.getPassword());
        //更新密码
        admin.setPassword(securePassword(passRequest.getNewPass()));
        //更新数据库
        this.adminMapper.updatePassword(admin);
    }

    private String securePassword(String password){
        String res = SecureUtil.md5(password + SALT);
        return res;
    }
}
